package org.example.dao;

import org.example.pojo.Manages;
import org.example.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class SelectManage {

    // 获取所有管理员信息
    public List<Manages> getManage() throws SQLException {
        // 使用try-with-resources确保资源被正确关闭
        try (Connection connection = JDBCUtils.getConnection();
             PreparedStatement ps = connection.prepareStatement("SELECT * FROM manage");
             ResultSet rs = ps.executeQuery()) {

            List<Manages> list = new ArrayList<>();
            while (rs.next()) {
                list.add(new Manages(
                        rs.getInt("id"),
                        rs.getString("username"),
                        rs.getString("password"),
                        rs.getString("name"),
                        rs.getTimestamp("create_time"),
                        rs.getTimestamp("update_time")
                ));
            }
            return list;
        } catch (SQLException e) {
            // 处理或记录异常
            throw e;
        }
    }

    // 添加管理员信息
    public boolean addManage(Manages manage) {
        String sql = "INSERT INTO manage (username, password, name, create_time, update_time) VALUES (?, ?, ?, ?, ?)";
        try (Connection conn = JDBCUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, manage.getUsername());
            pstmt.setString(2, manage.getPassword());
            pstmt.setString(3, manage.getName());
            pstmt.setTimestamp(4, new Timestamp(manage.getCreateTime().getTime()));
            pstmt.setTimestamp(5, new Timestamp(manage.getUpdateTime().getTime()));
            int affectedRows = pstmt.executeUpdate();
            return affectedRows > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 更新管理员信息
    public boolean updateManage(Manages manage) {
        String sql = "UPDATE manage SET username = ?, password = ?, name = ?, update_time = ? WHERE id = ?";
        try (Connection conn = JDBCUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, manage.getUsername());
            pstmt.setString(2, manage.getPassword());
            pstmt.setString(3, manage.getName());
            pstmt.setTimestamp(4, new Timestamp(manage.getUpdateTime().getTime()));
            pstmt.setInt(5, manage.getId());
            int affectedRows = pstmt.executeUpdate();
            return affectedRows > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 删除管理员信息
    public boolean deleteManage(int id) {
        String sql = "DELETE FROM manage WHERE id = ?";
        try (Connection conn = JDBCUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, id);
            int affectedRows = pstmt.executeUpdate();
            return affectedRows > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
